package com.fengkai.filepost.pdfoutstream.test.ODsolutions;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;

/**
 * @author Fengkai
 * @creat 2023/4/1
 */
public class IntegerCode {
    static char[] charArray = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'K',
            'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };


//29923 63666 43437
    public static void main (String[] args) {
        for (int j = 0; j < 5000; j++) {
            int i = (int) (Math.random() * 65535) + 1;
            System.out.println();
            if (!mymeThod(i).equals(solve(i))){
                System.err.println(i);
            }
        }

    }

    public static String mymeThod (int input) {
        //处理二进制数组
        int[] ints = new int[32];
        for (int i = 31; i >= 0; i--) {
            ints[i] += ((input >> (31 - i)) & 1);
        }
//        System.out.println(Arrays.toString(ints));
        //将ints分成5组 7位一组  不足补0
        int[] ints1 = new int[35];
        for (int i = 0; i < ints1.length; i++) {
            if (i > 2){
                ints1[i] = ints[i - 3];
            }
        }
//        System.out.println(Arrays.toString(ints1));
        StringBuilder[] intStr = new StringBuilder[5];
        intStr[0] = new StringBuilder("");
        intStr[1] = new StringBuilder("");
        intStr[2] = new StringBuilder("");
        intStr[3] = new StringBuilder("");
        intStr[4] = new StringBuilder("");
        for (int i = 0; i < 5; i++) {
            intStr[i].append( ints1[7 * i]).append( ints1[7 * i + 1]).append( ints1[7 * i + 2]).append( ints1[7 * i + 3])
                     .append( ints1[7 * i + 4]).append( ints1[7 * i + 5]).append( ints1[7 * i + 6]);
        }
//        System.out.println(Arrays.toString(intStr));
        //记录每组之后是否还有更多字节
        boolean  []dp = new boolean[5];
        ArrayList<String> hexList = new ArrayList<>();
        boolean find = false;
        for (int i = 0; i < intStr.length; i++) {
            if (!find){
                if (intStr[i].toString().contains("1")){
                    find = true;
                    dp[i] = true;
                    String s = Integer.toHexString(Integer.parseInt(("0" + intStr[i].toString()), 2));
                    s = s.length() == 2 ? s : "0" + s;
                    hexList.add(s.toUpperCase());
                }
            }
            if (find && i > 0 && dp[i - 1]){
                String s = Integer.toHexString(Integer.parseInt(("1" + intStr[i].toString()), 2));
                dp[i] = true;
                s = s.length() == 2 ? s : "0" + s;
                hexList.add( s.toUpperCase());
            }
        }
        String res = "";
        for (int i = hexList.size() - 1; i >= 0; i--) {
            res += hexList.get(i);
        }
        return res;
    }


    private static String solve(int number) {
        String numStr = Integer.toBinaryString(number);
        boolean statu;
        StringBuilder stringBuilder = new StringBuilder();
        // 这里需要先先找到低7位
        while (0 < numStr.length()) {
            if (7 < numStr.length()) {
                statu = true;
                // 这里2进制不足7位时补0
            } else {
                statu = false;
                int midIdx = 7 - numStr.length();
                for (int j = 0; j < midIdx; ++j) {
                    numStr = "0" + numStr;
                }
            }
            String subStr = numStr.substring(numStr.length() - 7);
            numStr = numStr.substring(0, numStr.length() - 7);
            if (statu) {
                subStr = "1" + subStr;
            } else {
                subStr = "0" + subStr;
            }
            String resultStr = Integer.toHexString(Integer.parseInt(subStr, 2));
            // 这里16进制不足2位补0
            if (2 > resultStr.length()) {
                resultStr = "0" + resultStr;
            }
            for (int j = 0; j < resultStr.length(); ++j) {
                char c = resultStr.charAt(j);
                if (c <= 'z' && c >= 'a') {
                    c = charArray[c - 'a'];
                }
                stringBuilder.append(c);
            }
        }
        // 每个字符如果是小写要转成大写
       return stringBuilder.toString();

    }

















    //返回二进制数组
    public static int[] interCut (int input) {
        int[] ints = new int[32];
        for (int i = 31; i >= 0; i--) {
            ints[i] += ((input >> (31 - i)) & 1);
        }
        System.out.println(Arrays.toString(ints));
        return ints;
    }

}
